package com.example.ck.controller;


import com.example.ck.entity.Laboratory;
import com.example.ck.service.LaboratoryService;
import com.example.ck.vo.ResultVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 实验室类型表(Laboratory)表控制层
 *
 * @author makejava
 * @since 2024-05-01 12:14:45
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("laboratory")
public class LaboratoryController {
    @Autowired
    private LaboratoryService laboratoryService;

    /**
     * 查询所有实验室
     *
     * @return
     */
    @GetMapping
    public ResultVo getAll() {
        List<Laboratory> allLaboratory = laboratoryService.getAllLaboratory();
        return new ResultVo(allLaboratory);
    }

    /**
     * 添加实验室
     */
    @PostMapping("/add")
    public ResultVo addLaboratory(@RequestBody Laboratory laboratory) {
        boolean b = laboratoryService.addLaboratory(laboratory);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("注册成功");
            vo.setSuccess(true);
            vo.setCode(200);
            vo.setData(laboratory);
        } else {
            vo.setMessage("注册失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 在指定实验室中添加新实验室类型
     */
    @PostMapping("/addType")
    public ResultVo addType(@RequestBody Map<String, String> map) {
        String laboratoryName = map.get("laboratoryName");
        String laboratoryType = map.get("laboratoryType");
        boolean b = laboratoryService.addLaboratoryToLaboratory(laboratoryName, laboratoryType);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("类型添加成功");
            vo.setSuccess(true);
            vo.setCode(200);
            vo.setData(laboratoryType);
        } else {
            vo.setMessage("类型添加失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据实验室名称删除实验室
     */
    @DeleteMapping("/deleteLaboratory")
    public ResultVo deleteLaboratory(String laboratoryName) {
        boolean b = laboratoryService.deleteLaboratory(laboratoryName);

        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("实验室删除成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("实验室删除失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据实验室名称删除实验室类型
     */
    @DeleteMapping("/deleteLaboratoryType")
    public ResultVo deleteLaboratoryType(@RequestParam String laboratoryName, String laboratoryType) {

        boolean b = laboratoryService.deleteLaboratoryType(laboratoryName, laboratoryType);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("删除成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("删除失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }
}
